package iscript.uiFramework.components.menu 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	import flash.utils.setTimeout;
	import iscript.uiFramework.components.button.BaseButton;
	import iscript.uiFramework.components.button.ButtonState; 
	import iscript.uiFramework.components.InteractiveUIComponent;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.UIFramework;
	import iscript.uiFramework.util.Scale9GridBitmap;

	public class ToolStripMenuPanel extends UIComponent
	{
		
		//GameLite.UIComponent.DefaultTheme.MenuPanel

		private var _menuPanelBitmpdata:BitmapData; 
		private var _menuPanelItemBgBmd:BitmapData;
		private var _menuPanelItemBg:Bitmap
		private var _menuPanelBitmap:Scale9GridBitmap;
		
		internal var control:UIComponent;
		private var _ToolStripItemGroup:Vector.<ToolStripItem>;
		
		
		public function ToolStripMenuPanel()
		{
			_ToolStripItemGroup = new Vector.<ToolStripItem>();			
			super ();
		}
		
		override protected function initialize():void 
		{
			_menuPanelBitmpdata =  UIFramework.resource.getBitmapData ("GameLite.UIComponent." + this.theme + ".MenuPanel");
			_menuPanelItemBgBmd =  UIFramework.resource.getBitmapData ("GameLite.UIComponent." + this.theme + ".MenuPanelBg"); 
			
			var _rect:Rectangle = UIFramework.resource.getElementRectangle("Menu","MenuPanel");
			_menuPanelBitmap = new Scale9GridBitmap (_menuPanelBitmpdata, _rect);			
			addChild (_menuPanelBitmap);
			
			_menuPanelItemBg = new Bitmap (_menuPanelItemBgBmd);
			_menuPanelItemBg.x = _menuPanelBitmap.x+1;
			_menuPanelItemBg.y = _menuPanelBitmap.y+2;
			addChild (_menuPanelItemBg);
			
			
			super.initialize();
		}
		
		
		public function addToolStripItem(item:ToolStripItem):ToolStripItem {
			     item.render (this);
				 _ToolStripItemGroup.push (item);
				 return item;
			}
		 
		
		public function addText (text:String,value:Object=null,icon:BitmapData=null):ToolStripLabel {
			 var item:ToolStripLabel = new ToolStripLabel ();
			     item.data = value;
			     item.text = text;
				 item.icon = icon;
				 item.render (this);
				 _ToolStripItemGroup.push (item);
				 return item;
			}
			
		public function addSeparator ():ToolStripSeparator {
			   var item:ToolStripSeparator = new ToolStripSeparator ();
			       item.render (this);
				   _ToolStripItemGroup.push (item);
				 return item;				   
			}
		
		override public function render(container:DisplayObjectContainer = null):void 
		{
			var len:int;
			for each (var label:ToolStripItem in _ToolStripItemGroup ) 
			{
				if (label is ToolStripLabel) {
					var length:Number = ToolStripLabel (label).text.length;
					if (length> len) {
							len = length;
							}
					}
			}
			
			var item:InteractiveUIComponent 
			for (var i:int = 0; i < _ToolStripItemGroup.length; i++) 
			{
				item =  InteractiveUIComponent (_ToolStripItemGroup[i])
				item.x = _menuPanelBitmap.x+1;
				item.width = ToolStripItem.blockindent+ len*8.5;
				
				if (i == 0) {
					item.y = item.height * i +2;	
					}else {	
					item.y = InteractiveUIComponent (_ToolStripItemGroup[i-1]).height +InteractiveUIComponent (_ToolStripItemGroup[i-1]).y
					}
				if (!(item as ToolStripSeparator))
				{
					item.ClickAfterHandler = function (e:ToolStripItem):void {
						   visible = false;
						   if(control is BaseButton)
						    BaseButton(control).state = ButtonState.NORMAL;
						}
				}
			}
			
			_menuPanelBitmap.width = item.width+4;
			_menuPanelBitmap.height = item.y+item.height +5 ;
			_menuPanelItemBg.height = _menuPanelBitmap.height-7;
			super.render(container);
		}
 
		
		
		
	}

}

 